// Simulate config options from your production environment by
// customising the .env file in your project's root folder.
require('dotenv').config();
// Require keystone
var keystone = require('keystone');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const next_app = next({ dev });
const next_handle = next_app.getRequestHandler();


//mobxReact.useStaticRendering(true);

// Initialise Keystone with your project's configuration.
// See http://keystonejs.com/guide/config for available options
// and documentation.
next_app
	.prepare()
	.then(() => {
		keystone.init({
			'name': 'test',
			'brand': 'test',
			'less': 'static',
			'static': 'static',
			'favicon': 'static/favicon.ico',
			'views': 'templates/views',
			'view engine': 'pug',
			'compress': true,
			'view cache': true,
			'auto update': true,
			'session': true,
			'session store': 'connect-mongo',
			'auth': true,
			'user model': 'User',
			'cookie secret': '7b39ff9acce4fafad83d176d6c6212c715d69f1fee15a96bdf5js83d588ad2a381h5sk72w6y3xb1h33570c5812dc40b27ddgs364f41panb235bx5282sd7ba0b1954ed322db0a666fe2b0b37e39ca04d16fcbd14b09dd9b206a',
			'session options': {
				cookie: { maxAge: 1000 * 60 * 60 * 24 }
			}
		});
		// Load your project's Models
		keystone.import('models');

		// Setup common locals for your templates. The following are required for the
		// bundled templates and layouts. Any runtime locals (that should be set uniquely
		// for each request) should be added to ./routes/middleware.js
		keystone.set('locals', {
			_: require('lodash'),
			env: keystone.get('env'),
			utils: keystone.utils,
			editable: keystone.content.editable,
		});

		// Load your project's Routes
		keystone.set('routes', require('./routes'));


		// Configure the navigation bar in Keystone's Admin UI
		keystone.set('nav', {
			posts: ['posts', 'post-categories'],
			galleries: 'galleries',
			enquiries: 'enquiries',
			users: 'users',
		});

		keystone.mongoose.plugin(schema => { schema.options.usePushEach = true });

		keystone.next_app = next_app;
		keystone.next_handle = next_handle;
		keystone.chapter_cache = {};
 
		// Start Keystone to connect to your database and initialise the web server
		keystone.start();
	})
	.catch((err) => {
		throw err;
	});
